<%@page import="registration.register"%>
<%@page import="org.w3c.dom.Document"%>
<%@page import="org.eclipse.jdt.internal.compiler.ast.ForeachStatement"%>
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" import="java.io.*"%>
<jsp:useBean id="reg" scope="session" class="registration.register" />
<!--main content example start-->
<div id="main">

<!-- //Code on this page is by Jonathan Axford unless otherwise noted -->
<%!
	//if your page needs to verify if the customer is logged in or not:

	//grab session attribute. If customer logged in, the string will be "true":
	
	
	//to test just this page without going through login, uncomment following:
	//private String sessionFlag = "true";
	
%><%
	if ((((String)session.getAttribute("login")) == "true"&&((String)request.getParameter("btnSubmit")) == null)/*&&(((String)request.getParameter("btnSubmit")) == null)*/)
	{
		//do what page needs to do if customer is logged in.
		//Form drawing will happen outside of this, and then the feilds will be populated if the user is logged in. --Jon
		String sessionUser = (String)session.getAttribute("customerid");
		String[] userDet = reg.getUser(sessionUser);
		%>
		<h2>Your account information</h2>
		<form name="registerform" method="get" action="">
		<table>
		<tr><td>First Name</td><td><input name="fName" type="text" value="<%out.print(userDet[0]); %>"/></td></tr>
		<tr><td>Last Name</td><td><input name="lName" type="text" value="<%out.print(userDet[1]); %>"/></td></tr>
		<tr><td>Address</td><td><input name="addr" type="text" value="<%out.print(userDet[2]); %>"/></td></tr>
		<tr><td>City</td><td><input name="city" type="text" value="<%out.print(userDet[3]); %>"/></td></tr>
		<tr><td>Province</td><td><input name="prov" type="text" value="<%out.print(userDet[4]); %>"/></td></tr>
		<tr><td>Postal Code</td><td><input name="postal" type="text" value="<%out.print(userDet[5]); %>"/></td></tr>
		<tr><td>Country</td><td><input name="country" type="text" value="<%out.print(userDet[6]); %>"/></td></tr>
		<tr><td>Home Phone</td><td><input name="hPhone" type="text" value="<%out.print(userDet[7]); %>"/></td></tr>
		<tr><td>Work Phone</td><td><input name="wPhone" type="text" value="<%out.print(userDet[8]); %>"/></td></tr>
		<tr><td>Email</td><td><input name="email" type="text" value="<%out.print(userDet[9]); %>"/></td></tr>
		<tr><td>Password</td><td><input name="passwd1" type="password"/></td></tr>
		<tr><td>Retype your password</td><td><input name="passwd2" type="password" /></td></tr>
		<tr><td>UserName:</td><td><%out.print(reg.getLogin(Integer.parseInt(sessionUser))); %></td></tr>
		<tr><td><input name="btnSubmit" class="button" type="submit" value="Update Changes" /></td><td><input name="Reset1" type="reset" value="reset" /></td></tr>
		</table>
		</form>
		<%
		
	}
	else if(((String)request.getParameter("btnSubmit")) == null)
	{
		//do what page needs to do if customer is NOT logged in.
		//I will need to come up with a method for determining if they came in from the registration link, or the accoount details link
		//If they came in via account details, and are not logged in, this will redirect them to the login page -- Jon
		%>
		<h2>Register below</h2>
		<form name="registerform" method="get" action="">
		<table>
		<tr><td>First Name</td><td><input name="fName" type="text"/></td></tr>
		<tr><td>Last Name</td><td><input name="lName" type="text"/></td></tr>
		<tr><td>Address</td><td><input name="addr" type="text"/></td></tr>
		<tr><td>City</td><td><input name="city" type="text"/></td></tr>
		<tr><td>Province</td><td><input name="prov" type="text"/></td></tr>
		<tr><td>Postal Code</td><td><input name="postal" type="text"/></td></tr>
		<tr><td>Country</td><td><input name="country" type="text"/></td></tr>
		<tr><td>Home Phone</td><td><input name="hPhone" type="text"/></td></tr>
		<tr><td>Work Phone</td><td><input name="wPhone" type="text"/></td></tr>
		<tr><td>Email</td><td><input name="email" type="text"/></td></tr>
		<tr><td>Password</td><td><input name="passwd1" type="password"/></td></tr>
		<tr><td>Retype your password</td><td><input name="passwd2" type="password"/></td></tr>
		<tr><td><input name="btnSubmit" class="button" type="submit" value="Submit"/></td><td><input name="Reset1" type="reset" value="reset" /></td></tr>
		</table>
		</form>
		<%
		
	}
	else
	{
		String fName = request.getParameter("fName");
		String lName = request.getParameter("lName");
		String addr = request.getParameter("addr");
		String city = request.getParameter("city");
		String prov = request.getParameter("prov");
		String postal = request.getParameter("postal").toUpperCase();
		String country = request.getParameter("country");
		String hPhone = request.getParameter("hPhone");
		String wPhone = request.getParameter("wPhone");
		String email = request.getParameter("email");
		String passwd1 = request.getParameter("passwd1");
		String passwd2 = request.getParameter("passwd2");
		if(passwd1.equals(passwd2))
		{	
			reg=new register( fName,  lName,  addr,  city, prov,
				 postal,  country,  hPhone,  wPhone,
				 email,  passwd1);
			if(reg.verify())
			{
				if(!reg.processReg()&&(((String)session.getAttribute("login")) == "true"))
				{
					String id = (String)session.getAttribute("customerid");
					reg.UpdateReg(id);
				}
			}
			else
			{
				boolean verified[] = reg.verifiedArray();
				out.println("<h3>Looks like you mistyped something</h3>");
				out.print("Please check the following: ");
				if(!verified[0])
					out.print("first name, ");
				if(!verified[1])
					out.print("last name, ");
				if(!verified[2])
					out.print("address, ");
				if(!verified[3])
					out.print("city, ");
				if(!verified[4])
					out.print("province, ");
				if(!verified[5])
					out.print("postal code, ");
				if(!verified[6])
					out.print("country, ");
				if(!verified[7])
					out.print("home phone, ");
				if(!verified[8])
					out.print("work phone, ");
				if(!verified[9])
					out.print("email, ");
				if(!verified[10])
					out.print("password. ");
			}
		
		
		
			if (reg.isSuccessfull())//If it works, log them in and do what happens on the login page
			{//this bit was done by Dagny, with modifications by Jon
				if(((String)session.getAttribute("login"))=="true")
				{
					response.sendRedirect("/TravelExpertsWeb/register.jsp");
					//return;
					//do the response.sendRedirect which isn't working via javascript:
					%>
						<script type="text/javascript">
							window.location = "http://localhost:8080/TravelExpertsWeb/register.jsp";
						</script>
					<%
				}
				else
				{
					//create a session for login that's true.
					session.setAttribute("login", "true");
					//create a session for id which contains the customerId
					session.setAttribute("customerid", reg.getID());
					//redirect to the rest of the website with the user logged in
					//this web page seems to be indicating why:
					//  http://www.coderanch.com/t/453816/JSP/java/response-sendRedirect-not-working
					response.sendRedirect("/TravelExpertsWeb/loginsuccess.jsp");
					//return;
					//do the response.sendRedirect which isn't working via javascript:
					%>
						<script type="text/javascript">
							window.location = "http://localhost:8080/TravelExpertsWeb/loginsuccess.jsp";
						</script>
					<%
				}
				
			}
			else
				if(reg.verify())
					out.print("<h3>Uhoh, something went wrong, are you already registered?</h3>");
		}
		else
		{
			out.print("<h3>Passwords do not match, please re-enter</h3>");
		}
	}

	
%>

				

		</div>
<!-- main ends -->	